package plenprojects.chatapi;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import org.springframework.web.client.RestTemplate;

public class ChatContext {
	
	protected static final String loginSuffix = "/sec/login";
	protected static final String logoutSuffix = "/sec/logout";
	protected static final String registerSuffix = "/sec/register";
	protected static final String tokenValidSuffix = "/sec/%s/isvalid";
	protected static final String chatRoomsSuffix = "/info/chatrooms";
	protected static final String roomUsersSuffix = "/info/chatroom/%s";
	protected static final String joinRoomSuffix = "/chat/%s/%s/join";//							/{token}/{roomname}/join
	protected static final String postMessageSuffix = "/chat/%s/%s/post";//							/{token}/{roomname}/post
	protected static final String recentMessagesSuffix = "/chat/%s/%s/%s/recent";//					/{token}/{roomname}/{after}/recent
	protected static final String inviteToRoomSuffix = "/chat/%s/%s/%s/invite";//					/{token}/{roomname}/{who}/invite
	protected static final String kickFromRoomSuffix = "/chat/%s/%s/%s/kick";//						/{token}/{roomname}/{who}/kick
	protected static final String createRoomSuffix = "/chat/%s/%s/create";//						/{token}/{roomname}/create
	
	private final String rootUrl;//http://localhost:8080/ChatService

	public ChatContext(String rootUrl) {
		
		this.rootUrl = rootUrl;
	}
	
	public Session login(String username, String password) throws ChatException, ConnectionException {
		
		RestTemplate rest = new RestTemplate();
		ServerMessage req = new ServerMessage();
		
		req.setParameter("username", username);
		req.setParameter("password", password);
		
		ServerMessage resp = null;
		
		try {
			
			resp = rest.postForObject(rootUrl + loginSuffix, req, ServerMessage.class);
			
		}
		catch(Exception e) {
			
			throw new ConnectionException();
		}
		
		Boolean success = (Boolean)resp.getParameter("success");
		
		if(!success) {
			
			String error = (String)resp.getParameter("error");
			
			throw new ChatException(error);
		}
		
		String token = (String)resp.getParameter("token");
		
		return new Session(rootUrl, token);
	}
	
	public Session register(String username, String password, String description, File image) throws ChatException, ConnectionException {
		
		RestTemplate rest = new RestTemplate();
		ServerMessage req = new ServerMessage();
		
		req.setParameter("username", username);
		req.setParameter("password", password);
		req.setParameter("description", description);
		
		if(image != null) {
			
			byte[] bytes = null;
			
			try {
				
				bytes = Files.readAllBytes(Paths.get(image.getAbsolutePath()));
				
			}
			catch(IOException e) {
				
				throw new ChatException(e);
			}
			
			req.setParameter("image", bytes);
		}
		
		ServerMessage resp = null;
		
		try {
			
			resp = rest.postForObject(rootUrl + registerSuffix, req, ServerMessage.class);
			
		}
		catch(Exception e) {
			
			throw new ConnectionException();
		}
		
		Boolean success = (Boolean)resp.getParameter("success");
		
		if(!success) {
			
			String error = (String)resp.getParameter("error");
			
			throw new ChatException(error);
		}
		
		return this.login(username, password);
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getChatRoomsNames() throws ConnectionException {
		
		RestTemplate rest = new RestTemplate();
		
		ServerMessage resp = rest.getForObject(rootUrl + chatRoomsSuffix, ServerMessage.class);
		
		try {
			
			resp = rest.getForObject(rootUrl + chatRoomsSuffix, ServerMessage.class);
			
		}
		catch(Exception e) {
			
			throw new ConnectionException();
		}
		
		return (List<String>)resp.getParameter("rooms");
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getUsernamesFromRoom(String room) throws ChatException, ConnectionException {
		
		RestTemplate rest = new RestTemplate();
		
		String suffix = String.format(roomUsersSuffix, room);
		
		ServerMessage resp = null;
		
		try {
			
			resp = rest.getForObject(rootUrl + suffix, ServerMessage.class);
			
		}
		catch(Exception e) {
			
			throw new ConnectionException();
		}
		
		Boolean success = (Boolean) resp.getParameter("success");
		
		if(!success) {
			
			String error = (String)resp.getParameter("error");
			
			throw new ChatException(error);
		}
		
		return (List<String>)resp.getParameter("users");
	}
}
